package com.li.youhappygo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.li.youhappygo.exception.GlobalException;
import com.li.youhappygo.mapper.UserAddressMapper;
import com.li.youhappygo.model.entity.user.UserAddress;
import com.li.youhappygo.model.vo.common.ResultCode;
import com.li.youhappygo.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserAddressServiceImpl implements UserAddressService {
    @Autowired
    UserAddressMapper userAddressMapper;

    @Override
    public PageInfo<UserAddress> getUserAddressById(Integer usersId,Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<UserAddress> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("userId",usersId);
        List<UserAddress> userAddresses = userAddressMapper.selectList(queryWrapper);
        PageInfo<UserAddress> pageInfo = new PageInfo<>(userAddresses);
        return pageInfo;
    }

    @Override
    public void deleteUserAddressByUsersId(Integer id) {
        if(userAddressMapper.selectOne(new QueryWrapper<UserAddress>().eq("id",id))==null){
            throw new GlobalException(ResultCode.DATA_ERROR);
        }
        userAddressMapper.delete(new QueryWrapper<UserAddress>().eq("id",id));
    }

    @Override
    public void updateUserAddress(UserAddress userAddress) {
        userAddressMapper.update(userAddress,new QueryWrapper<UserAddress>().eq("id",userAddress.getId()));
    }

    @Override
    public void addUserAddress(UserAddress userAddress) {
        userAddressMapper.insert(userAddress);
    }
}
